/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.cctv.handler;

import com.seari.bean.BaseClient;
import com.seari.bean.cctv.AgreInfo;
import com.seari.bean.cctv.Login;
import com.seari.bean.cctv.XMLBean;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.util.concurrent.GenericFutureListener;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 29120
 * 竞天操作逻辑版本
 */
public class CctvClient2 extends BaseClient
{
    protected static Logger logger = LoggerFactory.getLogger(CctvClient2.class);
    
    public CctvClient2()
    {
        super();
    }
    
    public CctvClient2(String host,int port)
    {
        this.host = host;
        this.port = port;
    }
    public CctvClient2(int port)
    {
        this("localhost", port);
    }
    

    
    @Override
    public void setup() throws InterruptedException
    {
        EventLoopGroup workerGroup = new NioEventLoopGroup();  
        //CctvClient client = this;
        try {  
            Bootstrap b = new Bootstrap();  
            b.group(workerGroup);  
            b.channel(NioSocketChannel.class);  
            b.remoteAddress(new InetSocketAddress(this.host, this.port));
            b.option(ChannelOption.SO_KEEPALIVE, true);  
            b.handler(new ChannelInitializer<SocketChannel>() {  
                @Override  
                public void initChannel(SocketChannel ch) throws Exception {
                      System.out.println("正在连接中……");
                    // xml解码器
                   ByteBuf delimiter = Unpooled.copiedBuffer("\r\n\r\n".getBytes());
                   ch.pipeline().addLast("delimiterDecoder", new  DelimiterBasedFrameDecoder(1024, delimiter));
                   ch.pipeline().addLast("cctvDecoder", new StringDecoder(Charset.forName("utf-8")));
                    
                    // xml编码器
                   ch.pipeline().addLast("cctvEncoder", new CctvEncoder());
                   ch.pipeline().addLast("cctvClientHandler", new CctvClientHandler());

                }  
            // Start the client.  
            });  
            ChannelFuture f = b.connect(host, port).sync();  
            System.out.println("连接成功");

            channel = f.channel();
            if(f.isSuccess())
            {
                logger.info("连接成功");
            }
            //f.channel().closeFuture().sync();  
            f.channel().closeFuture().addListener(new GenericFutureListener<ChannelFuture>(){
                @Override
                public void operationComplete(ChannelFuture f) throws Exception
                {
                    workerGroup.shutdownGracefully();
                }
            });
        } catch(Exception ex)
        {
            logger.warn(ex.getMessage());
        }
    }
    
    public void loginCCTV(String userName,String pwd, String ip ,int port)
    {
        //1表示登陆标志
        Login login = new Login(1 , userName , pwd, ip , port); 
        sendXml(login);
    }
    
    public void logoutCCTV()
    {
        //0表示登出标志
        XMLBean xmlBean = new XMLBean();
        Map<String,String> attrMap = new HashMap<>();
        attrMap.put("login", "0");
        xmlBean.setAttrMap(attrMap);
        sendXml(xmlBean);
    }
    
    //按下数字键
    public void numClick(int num)
    {
        AgreInfo ai = new AgreInfo(1, num, 1, "5");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        AgreInfo bi = new AgreInfo(1, num, 0, "5");
        sendXml(bi);
    }
   //按mult功能键
    public void multClick()
    {
        AgreInfo ai = new AgreInfo(1, 36, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    
   //按esc功能键
    public void escClick()
    {
        AgreInfo ai = new AgreInfo(1, 44, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);

    }
  //按上功能键
    public void upClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 11, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
   //按下功能键
    public void downClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 12, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
   //按左功能键
    public void leftClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 13, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
   //按右功能键
    public void rightClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 14, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
        //按下焦距+键
    public void focusAddClick(String stepNum){
        AgreInfo ai = new AgreInfo(1, 20, 1, stepNum);
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    //按左上功能健
    public void leftUpClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 15, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
    //按左下功能健
    public void leftDownClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 16, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
    //按右上功能健
    public void rightUpClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 17, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
    //按右下功能健
    public void rightDownClick(String stepVar)
    {
        AgreInfo ai = new AgreInfo(1, 18, 1, stepVar);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        ai.setStep("0");
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
             ex.printStackTrace();
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
        //按下焦距-键
    public void focusMinusClick(String stepNum){
        AgreInfo ai = new AgreInfo(1, 21, 1, stepNum);
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    //按下光圈+
    public void apertureAddClick(String stepNum){
        AgreInfo ai = new AgreInfo(1, 24, 1, stepNum);
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }   
    //按下光圈-
    public void apertureMinusClick(String stepNum){
        AgreInfo ai = new AgreInfo(1, 25, 1, stepNum);
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }      
    //按下方向停止键
    public void directStopClick()
    {
        AgreInfo ai = new AgreInfo(1, 10, 1, "0");
        sendXml(ai);
    }
    //按下DEVC键
    public void devcClick()
    {
        AgreInfo ai = new AgreInfo(1, 26, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException ex) {
            java.util.logging.Logger.getLogger(CctvClient2.class.getName()).log(Level.SEVERE, null, ex);
        }
        AgreInfo ai2 = new AgreInfo(1, 10, 1, "0");
        sendXml(ai2);
    }
    //按下CAM键
    public void camClick()
    {
        AgreInfo ai = new AgreInfo(1, 27, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    //按下MON键
    public void monClick()
    {
        AgreInfo ai = new AgreInfo(1, 28, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    //按下PT键
    public void ptClick()
    {
        AgreInfo ai = new AgreInfo(1, 41, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    //按下WIN键
    public void winClick()
    {
        AgreInfo ai = new AgreInfo(1, 36, 1, "0");
        sendXml(ai);
        try
        {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
        ai.setState(0);
        sendXml(ai);
    }
    
    
    public void sendXml(Object obj)
    {
        channel.writeAndFlush(obj);
    }

    @Override
    public void reset() throws Exception 
    {
        if(null != channel)
        {
            channel.close();
        }
        setup();
    }


    

    
    
}
